import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/Home.vue';
// 引入store
import { useUserStore } from '@/stores/use';

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: '/',
			name: 'home',
			component: HomeView
		},
		{
			path: '/about',
			name: 'about',
			component: () => import('../views/AboutView.vue')
		},
		{
			path: '/course',
			name: 'course',
			component: () => import('../views/CourseView.vue')
		},
		{
			path: '/course/detail',
			name: 'detail',
			component: () => import('../views/detailView.vue')
		},
		{
			path: '/login',
			name: 'login',
			component: () => import('../views/Login.vue')
		},
		{
			path: '/aes',
			name: 'aes',
			component: () => import('../views/aes.vue')
		},
		{
			path: '/course-play/:courseId/:chapterId',
			name: 'course-play',
			component: () => import('../views/courseplay.vue')
		},
		{
			path: '/cart',
			name: 'Cart',
			component: () => import('../views/cart.vue'),
			// 导航守卫
			beforeEnter: (to, from, next) => {
				console.log('cart登录ID', useUserStore().userInfo.id);
				if (useUserStore().userInfo.id) {
					// 如果有ID正常进行下一步
					next();
				} else {
					// 如果没有ID，跳转登录页面
					next('/login');
				}
			}
		},
		{
			path: '/confirmOrder',
			name: 'confirmOrder',
			component: () => import('../views/confirmOrder.vue'),
			// 导航守卫
			beforeEnter: (to, from, next) => {
				console.log('cart登录ID', useUserStore().userInfo.id);
				if (useUserStore().userInfo.id) {
					// 如果有ID正常进行下一步
					next();
				} else {
					// 如果没有ID，跳转登录页面
					next('/login');
				}
			}
		}
	]
});

export default router;
